package ar.com.ctrln.view.dashboard.mantenimiento.factibilidad;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventQueue;
import org.zkoss.zk.ui.event.EventQueues;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import ar.com.ctrln.control.mantenimiento.insumo.ConsultarInsumo;
import ar.com.ctrln.persistence.model.insumo.Insumo;
import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class SeleccionarInsumoController extends AbstractMinimizarVentana {

	private static final long serialVersionUID = -603091568601630985L;

	@WireVariable
	private ConsultarInsumo consultarInsumo;

	@Wire
	private Window wAgregarInsumos;

	@Wire
	private Listbox lbInsumos;

	@Wire("#txtFilter")
	private Textbox txtFilter;

	@Wire
	private Button btnSeleccionarInsumos;

	@Wire
	private Label footer_label;

	@Wire
	private Button btnVerDetalle;

	private ListModelList<Insumo> modelo;

	private Set<Insumo> insumos;

	private Set<Insumo> seleccion;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		insumos = new HashSet<Insumo>();

		for (Insumo insumo : consultarInsumo.listaInsumo()) {
			insumos.add(insumo);
		}

		modelo = new ListModelList<Insumo>();

		modelo.addAll(insumos);

		modelo.setMultiple(true);

		lbInsumos.setModel(modelo);

		lbInsumos.setMultiple(true);

		lbInsumos.setCheckmark(true);

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Listen("onClick = #btnSeleccionarInsumos")
	public void seleccionarInsumos() {

		seleccion = ((ListModelList) lbInsumos.getModel()).getSelection();

		EventQueue<Event> eq = EventQueues.lookup("SeleccionarInsumos",
				EventQueues.DESKTOP, false);

		eq.publish(new Event("onButtonClick", btnSeleccionarInsumos, seleccion));

		wAgregarInsumos.onClose();

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Listen("onChanging = #txtFilter")
	public void getFilter(Event event) {
		((Textbox) event.getTarget())
				.setValue(((org.zkoss.zk.ui.event.InputEvent) event).getValue());

		ListModelList model = new ListModelList(getFilterBy(
				txtFilter.getValue(), modelo.getInnerList()));

		model.setMultiple(true);

		lbInsumos.setModel(model);

		if (lbInsumos.getItemCount() > 0) {
			footer_label.setValue("");
			footer_label.setValue("Un total de " + lbInsumos.getItemCount()
					+ " Insumo/s");
		} else {
			footer_label.setValue("Ningun Resultado...");
			footer_label.setValue("");
		}
		((Textbox) event.getTarget()).setFocus(true);
		
		lbInsumos.setMultiple(true);
		lbInsumos.setCheckmark(true);
	}

	public static List<Insumo> getFilterBy(String condicion,
			Collection<Insumo> insumos) {
		List<Insumo> data = new ArrayList<Insumo>();
		for (Iterator<Insumo> i = insumos.iterator(); i.hasNext();) {
			Insumo tmp = i.next();
			if (tmp.getNombre().toLowerCase()
					.indexOf(condicion.trim().toLowerCase()) >= 0
					|| tmp.getDescripcion().toLowerCase()
							.indexOf(condicion.trim().toLowerCase()) >= 0
					|| tmp.getTipoInsumo().toLowerCase()
							.indexOf(condicion.trim().toLowerCase()) >= 0) {
				data.add(tmp);
			}
		}
		return data;
	}

}
